package com.xkdx.guangguang;

import java.util.Vector;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.application.MyApplication;
import com.xkdx.guangguang.fragment.brand.BrandFragment;
import com.xkdx.guangguang.module.cache.XkCacheManager;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class ManagerActivity extends FragmentActivity {
	private static String TAG = "ManagetActivity";
	public RadioButton shop_btn, brand_btn, home_btn, blog_btn, my_btn;
	public RelativeLayout mRl;
	WebView wv;
	private Vector<AsyncTask> mCurrentTasks;
	private DisplayMetrics metrics;
	private ImageView red_tip_icon;
	private int width = 0;
	private float scale;

	@Override
	protected void onCreate(Bundle arg0) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(arg0);
		setContentView(R.layout.base);
		if (IConstants.DisplayWidth == 0) {
			metrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(metrics);
			IConstants.DisplayWidth = metrics.widthPixels;
			IConstants.DisplayHeight = metrics.heightPixels;
		}
		mRl=(RelativeLayout)findViewById(R.id.fragment);
		
		// scale = getResources().getDisplayMetrics().density;
		// LayoutParams lp = my_btn.getLayoutParams();
		// LayoutParams im_lp = red_tip_icon.getLayoutParams();
		// im_lp.width = lp.width + dip2px(2,scale);
		// red_tip_icon.setLayoutParams(im_lp);

	}

	/**
	 * 将dip或dp值转换为px值，保证尺寸大小不变
	 * 
	 * @param scale
	 *            （DisplayMetrics类中属性density）
	 * @return
	 */
	public static int dip2px(float dipValue, float scale) {
		return (int) (dipValue * scale + 0.5f);
	}


	@Override
	protected void onStart() {
		super.onStart();
//		setOnSelected(IConstants.BottomBtnSelected);

	}

	@Override
	protected void onResume() {
		/*if (IConstants.isHasPush || IConstants.isFromSelf) {
			red_tip_icon.setVisibility(View.VISIBLE);
		} else {
			red_tip_icon.setVisibility(View.GONE);
		}*/

		super.onResume();

	}

	/*public void setOnSelected(int i) {
		switch (i) {
		case 1:
			shop_btn.setSelected(true);
			brand_btn.setSelected(false);
			home_btn.setSelected(false);
			blog_btn.setSelected(false);
			my_btn.setSelected(false);
			break;
		case 2:
			shop_btn.setSelected(false);
			brand_btn.setSelected(true);
			home_btn.setSelected(false);
			blog_btn.setSelected(false);
			my_btn.setSelected(false);
			break;
		case 3:
			shop_btn.setSelected(false);
			brand_btn.setSelected(false);
			home_btn.setSelected(true);
			blog_btn.setSelected(false);
			my_btn.setSelected(false);
			break;
		case 4:
			shop_btn.setSelected(false);
			brand_btn.setSelected(false);
			home_btn.setSelected(false);
			blog_btn.setSelected(true);
			my_btn.setSelected(false);
			break;
		case 5:
			shop_btn.setSelected(false);
			brand_btn.setSelected(false);
			home_btn.setSelected(false);
			blog_btn.setSelected(false);
			my_btn.setSelected(true);
			break;
		default:
			break;
		}
	}*/

	public void promptExit(final Context con) {
		// 创建对话框
		AlertDialog.Builder ab = new AlertDialog.Builder(con);
		ab.setTitle("提示");
		ab.setMessage("确定要退出吗");
		ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

				BrandFragment.shopBrandList = null;
				BrandFragment.lifeBrandList = null;
				BrandFragment.foodBrandList = null;
				BrandFragment.entertaimentBrandList = null;
				IConstants.BottomBtnSelected = 3;
				XkCacheManager.clearAllCache();
				SharePrefenceUtil su_city = new SharePrefenceUtil(con,
						IConstants.SP_CITY);
				su_city.setChoiceCityName("");

				// 结束百度定位服务

				((MyApplication) getApplication()).mLocClient
						.unRegisterLocationListener(((MyApplication) getApplication()).myLocationListenner);
				((MyApplication) getApplication()).mLocClient.stop();
				((MyApplication) getApplication()).myLocationListenner = null;
				((MyApplication) getApplication()).mLocClient = null;
				// 结束地图功能
				MyApplication app = (MyApplication) getApplication();
				if (app.mBMapManager != null) {
					app.mBMapManager.destroy();
					app.mBMapManager = null;
				}

				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				System.exit(0);
				android.os.Process.killProcess(android.os.Process.myPid());
				// finish();
			}
		});
		ab.setNegativeButton("取消", null);
		ab.show();

	}

/*	@Override
	public void onBackPressed() {
		UpLoadPhotoChoiceActivity.shopUpLoadPicture = null;
		int i = getSupportFragmentManager().getBackStackEntryCount();
		if (i <= 1) {
			
			if (IConstants.isHasPush && !IConstants.isHasSelfPush) {
				IConstants.isHasPush = false;
				finish();
			} else if(IConstants.isHasSelfPush){
				IConstants.isHasSelfPush = false;
				finish();
			}else {
				promptExit(ManagerActivity.this);
			}
			return;
		}
		if (i == 2 && IConstants.isRefresh) {
			promptExit(ManagerActivity.this);
			return;
		}
		if (i >= 3 && IConstants.isRefresh) {
			IConstants.isRefresh = false;
			getSupportFragmentManager().popBackStack();
		}

		super.onBackPressed();
	}*/

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// Log.i("WebActivity", "onKeyDown被调用");
		if (wv != null) {
			if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {

				wv.goBack();

				return true;
			}

		}

		return super.onKeyDown(keyCode, event);
	}

	public void setWebView(WebView wv) {
		this.wv = wv;
	}
	
	/*public void onClick(View v) {
		setOnSelected(IConstants.BottomBtnSelected);
		switch (v.getId()) {
		case R.id.radio_button0:
			if (IConstants.BottomBtnSelected != 1) {
				IConstants.BottomBtnSelected = 1;
				Intent intent = new Intent(ManagerActivity.this,
						ShopActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.radio_button1:
			if (IConstants.BottomBtnSelected != 2) {
				IConstants.BottomBtnSelected = 2;
				Intent intent1 = new Intent(ManagerActivity.this,
						BrandActivity.class);
				startActivity(intent1);
			}
			break;
		case R.id.radio_button2:
			if (IConstants.BottomBtnSelected != 3) {
				IConstants.BottomBtnSelected = 3;
				Intent intent2 = new Intent(ManagerActivity.this,
						HomeActivity.class);
				startActivity(intent2);
			}
			break;
		case R.id.radio_button3:
			if (IConstants.BottomBtnSelected != 4) {
				IConstants.BottomBtnSelected = 4;
				Intent intent3 = new Intent(ManagerActivity.this,
						CardActivity.class);
				startActivity(intent3);
			}
			break;
		case R.id.radio_button4:
			if (IConstants.BottomBtnSelected != 5) {
				IConstants.BottomBtnSelected = 5;
				Intent intent4 = new Intent(this, UserActivity.class);
				startActivity(intent4);
			}
			break;

		default:
			break;
		}

	}*/

	@Override
	protected void onDestroy() {
		if (mCurrentTasks != null) {
			for (AsyncTask task : mCurrentTasks) {
				if (task != null) {
					task.cancel(true);
				}
			}
			mCurrentTasks = null;
		}
		super.onDestroy();
	}

	public synchronized void addCurrentTask(AsyncTask mCurrentTask) {

		if (mCurrentTasks == null) {
			mCurrentTasks = new Vector<AsyncTask>(2);
		}
		mCurrentTasks.add(mCurrentTask);
	}

}
